package com.yonyou.component;

import com.yonyou.common.icon.SparrowIcons;
import com.yonyou.ui.GitMRFrom;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author .
 */
public class MultiMrComboBox extends JComponent implements ActionListener {
    private Color color_white = new Color(230, 230, 230);

    private LinkedHashMap<String, String> values;
    private MultiPopup popup;
    private JTextField editor;
    protected JButton arrowButton;

    public MultiMrComboBox() {

    }

    public void initComponent(LinkedHashMap<String, String> value) {
        values = value;
        this.setLayout(new BorderLayout());
        popup = new MultiPopup(values);

        editor = new JTextField();
        //editor.setFont(new Font(Res.FONT, Font.PLAIN, Res.FONT_SIZE_INPUT));
        editor.setEditable(false);
        //editor.setBorder(BorderFactory.createLineBorder(color_white, 1, true));
        editor.setPreferredSize(new Dimension(200, GitMRFrom.text_h));
        editor.addActionListener(this);
        //editor.setBackground(new Color(181, 181, 181));
        arrowButton = createArrowButton();
        arrowButton.setBorder(BorderFactory.createLineBorder(new Color(181, 181, 181), 1, true));
        arrowButton.setBackground(color_white);
        arrowButton.setPreferredSize(new Dimension(10, GitMRFrom.text_h));
        arrowButton.addActionListener(this);
        add(editor, BorderLayout.WEST);
        add(arrowButton, BorderLayout.CENTER);
    }

    /**
     * 获取选中的数据
     */
    public String[] getSelectedValues() {
        return popup.getSelectedValues();
    }

    /**
     * 设置需要选中的值
     */
    public void setSelectValues(Object[] selectvalues) {
        popup.setSelectValues(selectvalues);
        setText(selectvalues);
    }

    private void setText(Object[] values) {
        if ( values.length > 0 ) {
            String value = Arrays.toString(values);
            value = value.replace("[", "");
            value = value.replace("]", "");
            editor.setText(value);
        } else {
            editor.setText("");
        }
    }


    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if ( !popup.isVisible() ) {
            popup.show(this, 0, getHeight());
        }
    }


    protected JButton createArrowButton() {
        JButton button = new BasicArrowButton(BasicArrowButton.SOUTH, UIManager.getColor("ComboBox.buttonBackground"),
                UIManager.getColor("ComboBox.buttonShadow"), UIManager.getColor("ComboBox.buttonDarkShadow"),
                UIManager.getColor("ComboBox.buttonHighlight"));
        button.setName("ComboBox.arrowButton");
        button.setPreferredSize(new Dimension(20, GitMRFrom.text_h));
        return button;
    }

    //内部类MultiPopup
    public class MultiPopup extends JPopupMenu implements ActionListener {
        private LinkedHashMap<String, String> values;
        private java.util.List<JCheckBox> checkBoxList = new java.util.ArrayList<JCheckBox>();


        public MultiPopup(LinkedHashMap<String, String> value) {
            super();
            values = value;
            initComponent();
        }

        private void initComponent() {
            JPanel checkboxPane = new JPanel();
            //checkboxPane.setBackground(color);
            JPanel buttonPane = new JPanel();
//            this.setLayout(new BorderLayout());

            for (Map.Entry<String, String> entry : values.entrySet()) {
                JCheckBox temp = new JCheckBox(entry.getKey(), SparrowIcons.setting);
                //temp.setBackground(color);
                //temp.setFont(new Font(Res.FONT, Font.PLAIN, Res.FONT_SIZE_BTN));
                checkBoxList.add(temp);
            }
            checkboxPane.setPreferredSize(new Dimension(150, 170));
            //checkboxPane.setBounds(0, 0, 350, 170);

            checkboxPane.setLayout(new GridLayout(checkBoxList.size(), 0, 5, 5));
            for (JCheckBox box : checkBoxList) {
                box.addActionListener((ActionListener) changeEvent -> {
                    JCheckBox source = (JCheckBox) changeEvent.getSource();
                    if ( "全选".equals(source.getText()) ) {
                        if ( checkBoxList.get(0).isSelected() ) {
                            for (int i = 1; i < checkBoxList.size(); i++) {
                                if ( !checkBoxList.get(i).isSelected() ) {
                                    checkBoxList.get(i).setSelected(true);
                                    checkBoxList.get(i).setSelectedIcon(SparrowIcons.setting);
                                }
                            }
                        } else {
                            for (int i = 1; i < checkBoxList.size(); i++) {
                                if ( checkBoxList.get(i).isSelected() ) {
                                    checkBoxList.get(i).setSelected(false);
                                }
                            }
                        }
                    } else {
                        int check = 0;
                        for (int i = 1; i < checkBoxList.size(); i++) {
                            if ( checkBoxList.get(i).isSelected() ) {
                                check++;
                            }
                        }
                        checkBoxList.get(0).setSelected(checkBoxList.size() - 1 == check ? true : false);
                    }

                    setSelectValues(getSelectedValues());
                });

                checkboxPane.add(box);
            }
            //
            //commitButton = new JButton("确定");
            //commitButton.addActionListener(this);
            //commitButton.setBackground(Color.RED);
            //
            //cancelButton = new JButton("取消");
            //cancelButton.addActionListener(this);
            //
            jScrollPane = new JScrollPane();
            jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            jScrollPane.setBounds(0, 0, 350, 170);

            jScrollPane.getViewport().add(checkboxPane);


            jScrollPane.validate();

            this.add(jScrollPane, BorderLayout.WEST);
            //this.setBackground(color);
            //this.setBorder(BorderFactory.createLineBorder(color));
        }

        public void setSelectValues(Object[] values) {
            if ( values.length > 0 ) {
                for (int i = 0; i < values.length; i++) {
                    for (int j = 0; j < checkBoxList.size(); j++) {
                        if ( values[i].equals(checkBoxList.get(j).getText()) ) {
                            checkBoxList.get(j).setSelected(true);
                            checkBoxList.get(j).setSelectedIcon(SparrowIcons.setting);
                        }
                    }
                }
                setText(getSelectedValues());
            }
        }


        public String[] getSelectedValues() {
            java.util.List<String> selectedValues = new java.util.ArrayList<String>();
            if ( checkBoxList.get(0).isSelected() ) {
                int count = checkBoxList.size() - 1;
                selectedValues.add("全选 " + "(" + count + ")");
            } else {
                for (int i = 0; i < checkBoxList.size(); i++) {
                    if ( !checkBoxList.get(i).isSelected() && StringUtils.equals(checkBoxList.get(i).getText(), "全选") ) {
                        selectedValues.add("");
                        continue;
                    } else if ( checkBoxList.get(i).isSelected() ) {
                        for (Map.Entry<String, String> entry : values.entrySet()) {
                            if ( checkBoxList.get(i).isSelected() && StringUtils.equals(checkBoxList.get(i).getText(), entry.getKey()) ) {
                                selectedValues.add(String.valueOf(entry.getKey()));
                            }

                        }
                    }
                }
            }

            if ( "".equals(selectedValues.get(0)) ) {
                selectedValues.remove(0);
            }
            if ( 0 == selectedValues.size() ) {
                selectedValues.add("");
            }

            return selectedValues.toArray(new String[selectedValues.size()]);
        }


        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            Object source = arg0.getSource();
            if ( source instanceof JButton ) {
                JButton button = (JButton) source;
                if ( button.equals(commitButton) ) {
                    setText(getSelectedValues());
                    popup.setVisible(false);
                } else if ( button.equals(cancelButton) ) {
                    popup.setVisible(false);
                }
            }
        }

    }

    /**
     * 确定
     */
    private JButton commitButton;
    /**
     * 取消
     */
    private JButton cancelButton;

    private JScrollPane jScrollPane;

}

